<!--
 * @Author: anonymous
 * @Date: 2022-01-18 20:06:31
 * @LastEditors: anonymous
 * @LastEditTime: 2022-05-14 22:56:17
 * @Description: 
-->
<template>
  <div class="login-container">
    <div class="login-decoration"></div>
    <div class="login-box">
      <div class="system-logo">
        <i class="fa fa-wrench logo-icon"></i>
      </div>
      <div class="system-title">后勤报修系统</div>
      <div class="system-subtitle">便捷高效的社区公共设施报修平台</div>
      
      <div class="login-form">
        <el-form :model="form" :rules="rules" ref="loginForm" label-position="top">
          <el-form-item prop="username" class="custom-form-item">
            <div class="input-icon-wrapper">
              <i class="fa fa-user input-icon"></i>
              <el-input 
                v-model="form.username" 
                autocomplete="off" 
                placeholder="请输入用户名"
                :class="{'input-focused': focusedField === 'username'}" 
                @focus="focusedField = 'username'"
                @blur="focusedField = ''"
              />
              <span class="input-focus-line" :class="{'active': focusedField === 'username'}"></span>
            </div>
          </el-form-item>
          
          <el-form-item prop="password" class="custom-form-item">
            <div class="input-icon-wrapper">
              <i class="fa fa-lock input-icon"></i>
              <el-input 
                v-model="form.password" 
                type="password" 
                autocomplete="off" 
                placeholder="请输入密码" 
                show-password
                :class="{'input-focused': focusedField === 'password'}" 
                @focus="focusedField = 'password'"
                @blur="focusedField = ''"
              />
              <span class="input-focus-line" :class="{'active': focusedField === 'password'}"></span>
            </div>
          </el-form-item>
          
          <el-form-item prop="userType" class="custom-form-item">
            <div class="role-select-wrapper">
              <div class="role-options">
                <div 
                  class="role-option" 
                  :class="{ 'active': form.userType === 'user' }"
                  @click="form.userType = 'user'"
                >
                  <div class="role-icon-container">
                    <i class="fa fa-user-circle role-icon"></i>
                  </div>
                  <span>普通用户</span>
                </div>
                <div 
                  class="role-option"
                  :class="{ 'active': form.userType === 'worker' }"
                  @click="form.userType = 'worker'"
                >
                  <div class="role-icon-container">
                    <i class="fa fa-wrench role-icon"></i>
                  </div>
                  <span>维修工人</span>
                </div>
              </div>
            </div>
          </el-form-item>
          
          <el-form-item class="login-button-wrapper">
            <el-button type="primary" @click="onLogin" :loading="loading" class="login-button">
              <span v-if="!loading">登 录</span>
              <span v-else>登录中...</span>
            </el-button>
          </el-form-item>
        </el-form>
        
        <div class="register-link">
          还没有账号？ <router-link to="/register">立即注册</router-link>
        </div>
        
        <div v-if="errorMsg" class="error-msg">
          <i class="fa fa-exclamation-circle"></i>
          {{ errorMsg }}
        </div>
      </div>
      
      <div class="system-footer">
        © {{ new Date().getFullYear() }} 后勤报修系统 版权所有黄亮
      </div>
    </div>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus';
import { saveLoginInfo } from '@/js/public';

export default {
  name: 'AccountLogin',
  data() {
    return {
      form: {
        username: '',
        password: '',
        userType: 'user' // 默认为普通用户
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        userType: [{ required: true, message: '请选择角色类型', trigger: 'change' }]
      },
      loading: false,
      errorMsg: '',
      focusedField: '' // 新增：跟踪当前聚焦的输入框
    };
  },
  methods: {
    async onLogin() {
      this.errorMsg = '';
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) return;
        this.loading = true;
        try {
          const res = await this.axios.post('/api/login', {
            username: this.form.username,
            password: this.form.password,
            userType: this.form.userType // 发送用户选择的类型到后端
          });

          if ((res.data.code === 200 || res.data.code === 43010) && res.data.data && res.data.data.satoken) {
            // 使用修改后的saveLoginInfo函数保存用户信息
            saveLoginInfo(res.data.data);
            
            // 获取后端返回的用户类型
            const serverLoginType = res.data.data.loginType || 'user';
            
            // 检查用户选择的类型与后端返回的类型是否匹配，或者检查后端是否返回了提示消息
            if (res.data.data.roleMessage) {
              this.errorMsg = res.data.data.roleMessage;
              this.loading = false;
              
              // 自动修正用户类型
              setTimeout(() => {
                this.form.userType = serverLoginType;
                this.errorMsg = '';
                this.redirectUser(serverLoginType);
              }, 1500);
              return;
            }
            
            console.log('登录成功，用户类型', serverLoginType);
            ElMessage.success(`登录成功，您的身份是: ${serverLoginType === 'worker' ? '维修工' : '普通用户'}`);
            
            // 重定向用户
            this.redirectUser(serverLoginType);
          } else {
            this.errorMsg = res.data.msg || '登录失败';
          }
        } catch (e) {
          console.error('登录失败:', e);
          this.errorMsg = '网络错误或服务器异常';
        } finally {
          this.loading = false;
        }
      });
    },
    
    // 根据用户类型重定向到相应页面
    redirectUser(loginType) {
      if (loginType === 'worker') {
        console.log('正在跳转到维修工界面...');
        this.$router.replace({ name: 'worker_repairCenter' });
      } else {
        console.log('正在跳转到普通用户界面...');
        this.$router.replace({ name: 'user_repairCenter' });
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(125deg, #0ea5e9, #0c4a6e);
  background-size: 400% 400%;
  animation: gradient 15s ease infinite;
  position: relative;
  overflow: hidden;
  padding: 20px;
}

@keyframes gradient {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.login-decoration {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  
  &::before, &::after {
    content: "";
    position: absolute;
    border-radius: 50%;
    z-index: 0;
    opacity: 0.3;
  }
  
  &::before {
    top: -150px;
    right: -150px;
    width: 400px;
    height: 400px;
    background: radial-gradient(circle, rgba(255,255,255,0.3) 0%, rgba(255,255,255,0) 70%);
    animation: float 10s ease-in-out infinite;
  }
  
  &::after {
    bottom: -100px;
    left: -100px;
    width: 300px;
    height: 300px;
    background: radial-gradient(circle, rgba(255,255,255,0.2) 0%, rgba(255,255,255,0) 70%);
    animation: float 15s ease-in-out infinite reverse;
  }
}

@keyframes float {
  0% {
    transform: translateY(0) scale(1);
  }
  50% {
    transform: translateY(-20px) scale(1.05);
  }
  100% {
    transform: translateY(0) scale(1);
  }
}

.login-box {
  background: rgba(255, 255, 255, 0.95);
  padding: 40px 32px 32px 32px;
  border-radius: 16px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  backdrop-filter: blur(10px);
  width: 100%;
  max-width: 400px;
  position: relative;
  z-index: 2;
  border: 1px solid rgba(255, 255, 255, 0.18);
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.25);
  }
}

.system-logo {
  text-align: center;
  margin-bottom: 20px;
  width: 100%;
  
  .logo-icon {
    font-size: 3.5rem;
    color: #0284c7;
    background: linear-gradient(45deg, #0284c7, #38bdf8);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    animation: pulse 2s infinite;
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
  100% {
    transform: scale(1);
  }
}

.system-title {
  font-size: 2rem;
  font-weight: bold;
  text-align: center;
  margin-bottom: 10px;
  letter-spacing: 0.3rem;
  color: #0f172a;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.1);
  width: 100%;
}

.system-subtitle {
  font-size: 0.9rem;
  text-align: center;
  margin-bottom: 30px;
  color: #64748b;
  width: 100%;
}

.login-form {
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  
  .custom-form-item {
    margin-bottom: 20px;
    width: 100%;
    
    :deep(.el-form-item__label) {
      padding-bottom: 8px;
      font-weight: 500;
    }
  }
  
  .input-icon-wrapper {
    position: relative;
    width: 100%;
    
    .input-icon {
      position: absolute;
      left: 12px;
      top: 50%;
      transform: translateY(-50%);
      font-size: 16px;
      color: #64748b;
      z-index: 2;
      transition: all 0.3s ease;
    }
    
    :deep(.el-input) {
      width: 100%;
      
      .el-input__wrapper {
        padding-left: 40px;
        height: 50px;
        border-radius: 12px;
        box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05) !important;
        border: 1px solid #e2e8f0;
        transition: all 0.3s ease;
        
        &:hover {
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08) !important;
          border-color: #cbd5e1;
        }
        
        &.is-focus, &.input-focused {
          border-color: #0284c7;
          box-shadow: 0 0 0 2px rgba(2, 132, 199, 0.2) !important;
        }
        
        .el-input__inner {
          font-size: 16px;
          color: #1e293b;
          font-weight: 500;
          
          &::placeholder {
            color: #94a3b8;
            font-weight: normal;
          }
        }
      }
    }
    
    .input-focus-line {
      position: absolute;
      bottom: 0;
      left: 0;
      height: 2px;
      width: 0;
      background: #0284c7;
      transition: all 0.3s ease;
      
      &.active {
        width: 100%;
      }
    }
    
    &:focus-within {
      .input-icon {
        color: #0284c7;
      }
    }
  }
  
  .role-select-wrapper {
    width: 100%;
    
    .role-options {
      display: flex;
      justify-content: center;
      gap: 15px;
      margin-bottom: 10px;
      
      .role-option {
        flex: 1;
        padding: 15px;
        border-radius: 12px;
        border: 1px solid #e2e8f0;
        display: flex;
        flex-direction: column;
        align-items: center;
        cursor: pointer;
        transition: all 0.3s ease;
        position: relative;
        overflow: hidden;
        max-width: 160px;
        
        &::before {
          content: '';
          position: absolute;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background: #0284c7;
          opacity: 0;
          z-index: 0;
          transition: opacity 0.3s ease;
        }
        
        &:hover {
          border-color: #0284c7;
          transform: translateY(-2px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
          
          .role-icon-container {
            background-color: rgba(2, 132, 199, 0.1);
          }
        }
        
        &.active {
          background-color: #0284c7;
          border-color: #0284c7;
          color: white;
          transform: translateY(-3px);
          box-shadow: 0 8px 16px rgba(2, 132, 199, 0.3);
          
          .role-icon-container {
            background-color: rgba(255, 255, 255, 0.2);
            
            .role-icon {
              color: white;
            }
          }
        }
        
        .role-icon-container {
          position: relative;
          z-index: 1;
          width: 50px;
          height: 50px;
          border-radius: 50%;
          background-color: #f1f5f9;
          display: flex;
          align-items: center;
          justify-content: center;
          margin-bottom: 12px;
          transition: all 0.3s ease;
          
          .role-icon {
            font-size: 24px;
            color: #0284c7;
            transition: all 0.3s ease;
          }
        }
        
        span {
          position: relative;
          z-index: 1;
          font-size: 15px;
          font-weight: 500;
        }
      }
    }
  }
  
  .login-button-wrapper {
    margin-top: 30px;
    width: 100%;
    display: flex;
    justify-content: center;
    
    .login-button {
      width: 100%;
      height: 50px;
      font-size: 16px;
      font-weight: 600;
      border-radius: 12px;
      background: linear-gradient(135deg, #0284c7, #0ea5e9);
      border: none;
      letter-spacing: 1px;
      box-shadow: 0 5px 15px rgba(14, 165, 233, 0.3);
      transition: all 0.3s ease;
      position: relative;
      overflow: hidden;
      
      &::before {
        content: '';
        position: absolute;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100%;
        background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
        transition: left 0.7s ease;
      }
      
      &:hover {
        transform: translateY(-3px);
        box-shadow: 0 8px 20px rgba(14, 165, 233, 0.4);
        background: linear-gradient(135deg, #0369a1, #0284c7);
        
        &::before {
          left: 100%;
        }
      }
      
      &:active {
        transform: translateY(-1px);
        box-shadow: 0 3px 10px rgba(2, 132, 199, 0.3);
      }
    }
  }
}

.register-link {
  text-align: center;
  margin-top: 20px;
  font-size: 14px;
  color: #64748b;
  width: 100%;
  
  a {
    color: #0284c7;
    font-weight: 500;
    text-decoration: none;
    transition: all 0.2s;
    position: relative;
    padding-bottom: 2px;
    
    &::after {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      width: 0;
      height: 1px;
      background: #0284c7;
      transition: width 0.3s ease;
    }
    
    &:hover {
      color: #0ea5e9;
      
      &::after {
        width: 100%;
      }
    }
  }
}

.error-msg {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #ef4444;
  margin-top: 15px;
  padding: 12px;
  background-color: #fee2e2;
  border-radius: 10px;
  font-size: 14px;
  animation: shake 0.5s ease-in-out;
  border-left: 3px solid #ef4444;
  width: 100%;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-5px); }
  40%, 80% { transform: translateX(5px); }
}

.system-footer {
  margin-top: 30px;
  text-align: center;
  font-size: 12px;
  color: #64748b;
  width: 100%;
}

/* 添加响应式设计 */
@media (max-width: 480px) {
  .login-box {
    width: 100%;
    padding: 30px 20px;
    margin: 0 10px;
  }
  
  .system-title {
    font-size: 1.6rem;
    letter-spacing: 0.2rem;
  }
  
  .system-subtitle {
    font-size: 0.8rem;
  }
  
  .login-form {
    .input-icon-wrapper {
      :deep(.el-input) {
        .el-input__wrapper {
          height: 45px;
          padding-left: 35px;
        }
      }
      
      .input-icon {
        font-size: 14px;
        left: 10px;
      }
    }
    
    .role-select-wrapper {
      .role-options {
        gap: 10px;
        
        .role-option {
          padding: 12px 8px;
          
          .role-icon-container {
            width: 40px;
            height: 40px;
            
            .role-icon {
              font-size: 20px;
            }
          }
          
          span {
            font-size: 13px;
          }
        }
      }
    }
    
    .login-button-wrapper {
      .login-button {
        height: 45px;
        font-size: 15px;
      }
    }
  }
}
</style>

